﻿using System;
using System.Windows;
using System.Windows.Data;

using Supremacy.Diplomacy;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for StipulationEditor.xaml
    /// </summary>
    public partial class StipulationEditor
    {
        #region Fields
        private readonly Stipulation _stipulation;
        #endregion

        #region Constructors
        public StipulationEditor(Stipulation stipulation, DiplomacyContext context)
        {
            if (stipulation == null)
                throw new ArgumentNullException("stipulation");
            if (context == null)
                throw new ArgumentNullException("context");
            InitializeComponent();
            if (stipulation.IsDurationFixed)
            {
                DurationEditor.Visibility = Visibility.Collapsed;
                BindingOperations.ClearAllBindings(DurationEditor);
            }
            _stipulation = stipulation;
            this.DataContext = stipulation;
            if (stipulation.HasParameter)
            {
                ParameterList.ItemsSource = stipulation.GetParameterList(context);
            }
            else
            {
                ParameterList.Visibility = Visibility.Collapsed;
            }
        }
        #endregion

        #region Methods
        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            if (_stipulation.IsParameterValid
                && (_stipulation.IsDurationFixed || (this.DurationEditor.Duration != Stipulation.InvalidDuration)))
            {
                Window window = this.Parent as Window;
                if (window != null)
                {
                    window.DialogResult = true;
                }
            }
        }
        #endregion
    }
}